package utils;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * Created by zyb on 14-12-11.
 */
public class DBUtil {

    public static Connection getConnection() throws Exception{
        final String url = "jdbc:mysql://localhost:3306/web?useUnicode=true&characterEncoding=utf-8";
        final String username = "root";
        final String password = "root";
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url,username,password);
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
        return connection;
    }

    public static void close(Connection connection){
        if(connection != null){
            try {
                connection.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    //测试
//    public static void main(String[] args) throws Exception{
//        System.out.println(getConnection());
//    }

}
